package com.efamily.ewallet.client;

import java.util.List;

import com.efamily.ewallet.client.account.bean.AccountBean;
import com.efamily.ewallet.client.balance.service.BalanceServiceAsync;
import com.efamily.ewallet.client.balance.widget.BalanceGridWidget;
import com.efamily.ewallet.client.common.bean.CommentBean;
import com.efamily.ewallet.client.common.widget.CommentsTreeWidget;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class BussinessWidget extends Composite {
	private ContentPanel mainPanel = new ContentPanel();
	private ContentPanel centerPanel = new ContentPanel();
	private ContentPanel southPanel = new ContentPanel();
	private ContentPanel eastPanel = new ContentPanel();
	private AccountBean accountBean = new AccountBean();

	public BussinessWidget(AccountBean accountBean) {
		this.accountBean = accountBean;
		mainPanel.setHeaderVisible(false);
		final BorderLayout borderLayout = new BorderLayout();
		mainPanel.setLayout(borderLayout);
		mainPanel.setBodyBorder(false);
		buildCenterPanel();
		buildSouthPanel();
		//buildEastPanel();
		southPanel.addListener(Events.Render, new Listener<BaseEvent>(){
			public void handleEvent(BaseEvent be) {
				//borderLayout.show(LayoutRegion.SOUTH);
				//borderLayout.collapse(LayoutRegion.SOUTH);
			}
		});
		
		initComponent(mainPanel);
	}

	private void buildCenterPanel() {
		centerPanel.setLayout(new FitLayout());
		centerPanel.setBodyBorder(false);
		centerPanel.setHeaderVisible(false);
		centerPanel.add(new BalanceGridWidget(accountBean));

		BorderLayoutData centerPanelData = new BorderLayoutData(
				LayoutRegion.CENTER);
		centerPanelData.setSplit(true);
		mainPanel.add(centerPanel, centerPanelData);
	}

	private void buildSouthPanel() {
		southPanel.setLayout(new FitLayout());
		southPanel.setBodyBorder(false);
		southPanel.setHeading("Expense&Profit");
		southPanel.add(new ExpensesAndProfitsWidget(accountBean));

		BorderLayoutData southPanelData = new BorderLayoutData(
				LayoutRegion.SOUTH);
		southPanelData.setSplit(true);
		southPanelData.setSize(400);
		southPanelData.setCollapsible(true);
		mainPanel.add(southPanel, southPanelData);
	}

	private void buildEastPanel() {
		eastPanel.setLayout(new FitLayout());
		eastPanel.setHeaderVisible(false);
		CommentsTreeWidget commentsTree = new CommentsTreeWidget() {
			@Override
			public void loadData(Object loadConfig,
					AsyncCallback<List<CommentBean>> callback) {
				BalanceServiceAsync.instance.getCommentBeans(accountBean, (CommentBean)loadConfig, callback);
			}
		};
		BorderLayoutData eastPanelData = new BorderLayoutData(LayoutRegion.EAST);
		eastPanelData.setSplit(true);
		eastPanel.add(commentsTree);
		mainPanel.add(eastPanel, eastPanelData);
	}
}
